# -*- coding: UTF-8 -*-
# 路虎保养套餐价格
# Python Version: 2.7.1

import requests
from lxml import etree
import xlwt
import re
import time
import sys
# 解决编码问题
reload(sys)
sys.setdefaultencoding('utf-8')

class LandRover_Maintenance_Price(object):

    def __init__(self):
        self.base_url = "https://www.landrover.com.cn"
        self.one_urllist = ["https://www.landrover.com.cn/ownership/service-package/range-rover.html",
                            "https://www.landrover.com.cn/ownership/service-package/range-rover-sport.html",
                            "https://www.landrover.com.cn/ownership/service-package/range-rover-evoque.html",
                            "https://www.landrover.com.cn/ownership/service-package/new-discovery.html",
                            "https://www.landrover.com.cn/ownership/service-package/discovery.html",
                            "https://www.landrover.com.cn/ownership/service-package/discovery-sport.html",
                            "https://www.landrover.com.cn/ownership/service-package/free-lander.html",
                            "https://www.landrover.com.cn/ownership/service-package/velar.html",
                            ]
        self.headers = {
            "Referer": "https://www.landrover.com.cn/ownership/service-package/index.html",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
        }
        # self.re_item_a = "基础尊护A套餐保养价格(.*?)</p>"
        # self.re_item_b = "基础尊护B套餐保养价格(.*?)</p>"
        self._workbook = xlwt.Workbook(encoding='utf-8')
        self._worksheet = self._workbook.add_sheet('Worksheet')
        self.rows = 0  # 表格计数

    def parse_url(self, url):
        response = requests.get(url=url, headers=self.headers, timeout=30, allow_redirects=False, verify=False)
        return response

    def get_HTML(self, content):
        html = etree.HTML(content)
        return html

    def save_xls(self, dict):
        # 写A套餐
        self._worksheet.write(self.rows, 0, dict["cars_title"])
        self._worksheet.write(self.rows, 1, "A")
        self._worksheet.write(self.rows, 2, dict["item_a"])
        self.rows += 1
        # 写B套餐
        self._worksheet.write(self.rows, 0, dict["cars_title"])
        self._worksheet.write(self.rows, 1, "B")
        self._worksheet.write(self.rows, 2, dict["item_b"])
        self.rows += 1
        # 写常规项
        for part in dict["item_c"]:
            self._worksheet.write(self.rows, 0, dict["cars_title"])
            self._worksheet.write(self.rows, 1, part["part"])
            self._worksheet.write(self.rows, 2, part["price"])
            self.rows += 1

    def run(self):

        # 写表头
        self._worksheet.write(self.rows, 0, "车型")
        self._worksheet.write(self.rows, 1, "套餐")
        self._worksheet.write(self.rows, 2, "价格")
        self.rows += 1

        # 第一步,循环初始url入口,
        for one_url in self.one_urllist:
            response = self.parse_url(one_url)
            one_html = self.get_HTML(response.content.decode("utf8"))

            # 获取当前车款所有车型li列表
            one_li_list = one_html.xpath("//div[@class='model DropdownNav mobileLight']/ul/li")

            # 遍历车型标签列表,获取每个车型url入口
            for li in one_li_list:
                time.sleep(1)
                cars_title = li.xpath("./@data-model")[0]
                print cars_title
                two_url = li.xpath("./a/@href")
                two_url = self.base_url + two_url[0]

                response = self.parse_url(two_url)
                # response = requests.get(url=two_url, headers=self.headers)
                # with open("./two.html", 'w') as f:
                #     f.write(response.content.decode("utf8"))
                two_html = self.get_HTML(response.content.decode("utf8"))

                item_a = re.findall("基础尊护A套餐保养价格(.*?)<", response.content)[0]
                item_b = re.findall("基础尊护B套餐保养价格(.*?)<", response.content)[0]

                # 进行数据清洗
                item_a = item_a.replace("&nbsp;¥", "")
                item_a = item_a.replace(" ", "")
                item_a = item_a.replace(":", "")
                item_a = item_a.replace("：", "")
                item_a = item_a.strip()

                item_b = item_b.replace("&nbsp;¥", "")
                item_b = item_b.replace(" ", "")
                item_b = item_b.replace(":", "")
                item_b = item_b.replace("：", "")
                item_b = item_b.strip()


                print item_a, "a套餐"
                print item_b, "b套餐"

                # 获取常规更换项目列表tr_list
                tr_list = two_html.xpath("//tbody/tr")

                part_price_list = []
                for tr in tr_list:
                    part = tr.xpath("./th/text()")[0]
                    price = tr.xpath("./td/text()")[0]

                    # 数据清洗
                    price = price.replace("¥", "")
                    print part, price
                    part_price_list.append(dict(part=part,price=price))

                result_dict = {"cars_title": cars_title, "item_a":item_a, "item_b": item_b, "item_c":part_price_list}

                # 保存进xls
                self.save_xls(result_dict)

        self._workbook.save("land_rover_item_price.xls")
        print ">>xls DONE"




if __name__ == '__main__':
    tools = LandRover_Maintenance_Price()
    tools.run()